package com.lgj.web.route.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgj.core.UserHelper;
import com.lgj.exception.BusinessException;
import com.lgj.result.ResultBody;
import com.lgj.result.ResultCode;
import com.lgj.web.proxy.GateWayServerService;
import com.lgj.web.route.mapper.GatewayRouteMapper;
import com.lgj.web.route.model.GatewayRoute;
import com.lgj.web.route.service.GateWayRouteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author: guangjie.liao
 * @Date: 2022/10/28 18:34
 * @Description:
 */
@Service
public class GateWayRouteServiceImpl implements GateWayRouteService {
    @Resource
    private GateWayServerService gateWayServerService;

    @Resource
    private GatewayRouteMapper gatewayRouteMapper;


    @Override
    public ResultBody addRule(GatewayRoute gatewayRoute) {
        addParamCheck(gatewayRoute);
        checkGateWayRouteName(gatewayRoute.getRouteName());
        checkGateWayRouteServiceId(gatewayRoute.getServiceId());

        gatewayRoute.setCreateTime(new Date());
        gatewayRouteMapper.insert(gatewayRoute);
        //刷新路由
        gateWayServerService.refresh();
        return ResultBody.success();
    }

    @Override
    public ResultBody queryRule(String routeName, String serverId,Integer pageSize,Integer pageIndex) {
        System.out.println("当前登录用户信息："+JSONObject.toJSONString(UserHelper.getUser()));
        PageHelper.startPage(pageIndex,pageSize);
        List<GatewayRoute> list = gatewayRouteMapper.queryList(routeName,serverId);
        PageInfo page = new PageInfo(list);
        return ResultBody.success(page);
    }

    @Override
    public ResultBody getRule(Long routeId) {
        if (routeId == null){
            throw new BusinessException(ResultCode.FAIL,"查询失败：routeId不能为空！");
        }
        return ResultBody.success(gatewayRouteMapper.selectByPrimaryKey(routeId));
    }

    @Override
    public ResultBody editRule(GatewayRoute gatewayRoute) {
        addParamCheck(gatewayRoute);
        checkGateWayRouteName(gatewayRoute.getRouteName());
        checkGateWayRouteServiceId(gatewayRoute.getServiceId());

        gatewayRoute.setUpdateTime(new Date());
        gatewayRouteMapper.updateByPrimaryKeySelective(gatewayRoute);
        //刷新路由
        gateWayServerService.refresh();
        return ResultBody.success();
    }

    @Override
    public ResultBody deleteRule(Long routeId) {
        if (routeId == null){
            throw new BusinessException(ResultCode.FAIL,"查询失败：routeId不能为空！");
        }
        gatewayRouteMapper.deleteByPrimaryKey(routeId);
        //刷新路由
        gateWayServerService.refresh();
        return ResultBody.success();
    }

    private void addParamCheck(GatewayRoute gatewayRoute){
        if (StringUtils.isEmpty(gatewayRoute.getRouteName())){
            throw new BusinessException(ResultCode.FAIL,"路由名称不能为空！");
        }
        if (StringUtils.isEmpty(gatewayRoute.getServiceId())){
            throw new BusinessException(ResultCode.FAIL,"路由服务不能为空！");
        }
        if (StringUtils.isEmpty(gatewayRoute.getPredicate())){
            throw new BusinessException(ResultCode.FAIL,"路由断言不能为空");
        }
    }

    private void checkGateWayRouteName(String routeName){
        GatewayRoute route = gatewayRouteMapper.selectByRouteName(routeName);
        if (route != null){
            throw new BusinessException(ResultCode.FAIL,"路由名称已存在！");
        }
    }

    private void checkGateWayRouteServiceId(String serviceId){
        GatewayRoute route = gatewayRouteMapper.selectByServiceId(serviceId);
        if (route != null){
            throw new BusinessException(ResultCode.FAIL,"路由服务名已存在！");
        }
    }
}
